var db = require('./../../conn/db');
var $sql = require('./../../conn/apisql');
var md5 = require('md5-node');
// npm i -S time-formater  //时间格式
var dtime = require('time-formater');


class Api {
	// 构造函数
	constructor(){
	};
	//登录
	login(req, res, next) {
		let params = req.body;
		db.query($sql.user.login, [md5(params.pass), params.ccount], (err,result) => {
			if(err) {
				return res.sendStatus(500);
			}
			if(!result[0]){
				res.json({code: 1, msg: '账号或密码错误'});
			} else {
				res.json({code: 0, msg: '登录成功',data: result[0]});
			}
		})
	};
	// 检测账号是否已被注册
	verifyCcount (req, res, next) {
		let ccount = req.body.ccount;
		db.query($sql.user.find, [ccount], (err, result) => {
			if (err) {
				return res.sendStatus(500);
			}
			if(result.length > 0) {
				res.json({code: 2, msg: '该账号已被注册！请重新输入'})
			} else {
				next();
			}
		});
	}
	//注册
	register(req, res, next) {
		let date = dtime().format('YYYY-MM-DD HH:mm:ss');
		let d = req.body;
		db.query($sql.user.add, [d.ccount, d.u_name, md5(d.pass), 2, d.u_img, date, md5(d.ccount)], (err, result) => {
			if (err) {
				return res.sendStatus(500);
			}
			res.json({code: 0, msg: '注册成功！'});
		})
	}
	// token验证
	verToken(req, res, next) {
		let t = req.body.token;
		db.query($sql.user.vertoken, [t], (err, result) => {
			if (err) {
				return res.sendStatus(500);
			}
			if(!result[0]) {
				res.json({code: 401, msg: '用户状态非法！'})
			} else {
				next();
			}
		})
	};
	// 发布mimi
	issue (req, res, next) {
		let d = req.body;
		let date = dtime().format('YYYY-MM-DD HH:mm:ss');
		db.query($sql.message.add, [d.content, d.u_id, d.type, date], (err, result) => {
			if(err) {
				return res.sendStatus(500);
			}
			res.json({code: 0, msg: '发布成功'});
		});
	}
	// 修改个人信息
	upUserInfo (req, res, nexr) {
		let d = req.body;
		db.query($sql.user.update1, [d.name, d.sex, d.u_img, d.u_id], (err, result) => {
			if (err) {
				return res.sendStatus(500);
			}
			console.log('修改结果：' + JSON.stringify(res.result));
			res.json({code: 0, msg: '修改成功！'});
		});
	}
	// 获取用户评论和点赞数量
	getUserNum (req, res, next) {
		let d = req.body;
		console.log('得到数据；' + JSON.stringify(d));
		db.query($sql.user.get_num, [d.u_id], (err, result) => {
			if(err) {
				return res.sendStatus(500);
			}
			res.json({code: 0, msg: 'success', data: result[0]});
		});
	}
	// 修改用户密码
	setPwd(req, res, next) {
		let d = req.body;
		db.query($sql.user.up_pwd, [md5(d.new_pass), md5(d.pass), d.u_id], (err, result) => {
			if(err) {
				return res.sendStatus(500);
			}
			console.log(result)
			if(result.affectedRows > 0){
				res.json({code: 0, msg: '修改成功'});
			} else {
				res.json({code: 1, msg: '原密码错误！'});
			}
		});
	}
	// 获取首页数据
	index (req, res, next) {
		let d = req.body;
		let page =  parseInt(d.page) || 1;
		let pagesize = d.pagesize || 30;
		let last_page = page - 1;
		db.query($sql.message.find, [pagesize, last_page * pagesize], (err, result) => {
			if (err) {
				return res.sendStatus(500);
			}
			res.json({
				code: 0, 
				msg: 'success', 
				data: result, 
				total: result[0] ? result[0].count : 0,
				total_page: result[0] ? Math.ceil(result[0].count / pagesize) : 0,
				current_page: page
			});
		})
	};
	// 我的秘密
	myMimi(req, res, next){
		let d = req.body;
		let page =  parseInt(d.page) || 1;
		let pagesize = d.pagesize || 30;
		let last_page = page - 1;
		db.query($sql.message.find1, [d.u_id, pagesize, last_page * pagesize], (err, result) => {
			if (err) {
				return res.sendStatus(500);
			}
			res.json({
				code: 0, 
				msg: 'success', 
				data: result, 
				total: result[0] ? result[0].count : 0,
				total_page: result[0] ? Math.ceil(result[0].count / pagesize) : 0,
				current_page: page
			});
		})
	}
	// 获取秘密详情
	messageDet(req, res, next){ 
		let d = req.body;
		db.query($sql.message.mess_det, [d.u_id, d.m_id, d.m_id], (err, result) => {
			if (err) {
				return res.sendStatus(500);
			}
			if(result[0]){
				res.json({code: 0, msg: 'success', data: result[0]})
			} else {
				return res.sendStatus(404);
				// res.json({code: 1, msg: '内容不存在或者已被删除'});
			}
		});
	};
	// 点赞功能
	toDot(req, res, next) {
		let d = req.body;
		db.query($sql.dot.add, [d.u_id, d.m_id], (err, result) => {
			if(err){
				return res.sendStatus(500);
			}
			if(result.affectedRows > 0){
				res.json({code: 0, msg: '点赞成功'});
			} else {
				res.json({code: 1, msg: '点赞失败'});
			}
		});
	}
	// 取消点赞
	delDot(req, res, next){
		let d = req.body;
		db.query($sql.dot.remove, [d.u_id, d.m_id], (err, result) => {
			if(err){
				return res.sendStatus(500);
			}
			if(result.affectedRows > 0){
				res.json({code: 0, msg: '已取消点赞'});
			} else {
				res.json({code: 1, msg: '操作失败'});
			}
		});
	}
	// 获取评论列表
	getComment(req, res, next) {
		let d = req.body;
		db.query($sql.comments.find, [d.m_id], (err, result) => {
			if (err) {
				return res.sendStatus(500);
			}
			if(result[0]){
				res.json({code: 0, msg: 'success', data: result})
			} else {
				res.json({code: 1, msg: '暂无相关评论', data: result})
			}
		});
	}
	// 评论
	toComment(req, res, next) {
		let d = req.body;
		let date = dtime().format('YYYY-MM-DD HH:mm:ss');
		db.query($sql.comments.add, [d.u_id, d.m_id, d.content, date], (err, result) => {
			if(err){
				return res.status(500).send(err);
			}
			if(result.affectedRows > 0){
				res.json({code: 0, msg: '评论成功！'});
			} else {
				res.json({code: 1, msg: '评论失败'});
			}
		});
	};
	// 删除评论
	delComment(req, res, next){
		let d = req.body;
		db.query($sql.comments.remove, [d.c_id], (err, result) => {
			if(err){
				return res.sendStatus(500);
			}
			if(result.affectedRows > 0){
				res.json({code: 0, msg: '已删除'});
			} else {
				res.json({code: 1, msg: '删除失败'});
			}
		});
	}
	// 删除mimi
	delMessage(req, res, next) {
		let d = req.body;
		db.query($sql.message.remove, [d.m_id], (err, result) => {
			if(err){
				return res.sendStatus(500);
			}
			if(result.affectedRows > 0){
				res.json({code: 0, msg: '已删除'});
			} else {
				res.json({code: 1, msg: '删除失败'});
			}
		});
	}
}

module.exports = new Api();